package com.nttuan.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.nttuan.model.DataBaseHelper;
import com.nttuan.model.dao.service.TaskService;
import com.nttuan.model.dao.service.TrackService;
import com.nttuan.model.entity.Task;
import com.nttuan.model.entity.Track;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by NT Tuan on 10/21/2014.
 */
public class TrackDao implements TrackService{
    private DataBaseHelper db;
    private TaskService taskService;
    private static TrackDao trackDao;

    public static TrackDao getInstance(Context context){
        if(trackDao == null)
            trackDao = new TrackDao(context);
        return trackDao;
    }
    public TrackDao(Context context){
        try{
            taskService = TaskDao.getInstance(context);
            db = new DataBaseHelper(context);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Track> tracks() {
        List<Track> result = new ArrayList<Track>();
        db.openDataBase();
        Cursor cursor = db.select(true,Track.TABLE_NAME,null,null,null,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;
    }

    @Override
    public boolean create(Track track) {
        db.openDataBase();
        ContentValues values  = new ContentValues();
        values.put(Track.ACTION, track.getActon());
        values.put(Track.MESSAGE, track.getMessage());
        values.put(Track.IS_ACTIVE, track.isActive());
        values.put(Track.COMMIT, track.isCommit());
        values.put(Track.LAST_TIME, Support.toString(track.getLastTime()));
        values.put(Task.TASK_ID, track.getTask().getTaskId());
        boolean result = db.insert(Track.TABLE_NAME, values);
        db.close();
        return  result;
    }

    @Override
    public boolean remove(int trackId) {
        db.openDataBase();
        String whereClause = Track.TRACK_ID + "=?";
        String[] args = {trackId+""};
        boolean result = db.delete(Track.TABLE_NAME, whereClause, args);
        db.close();
        return  result;
    }

    @Override
    public boolean active(int trackId, boolean isActive) {
        db.openDataBase();
        ContentValues values  = new ContentValues();
        values.put(Track.IS_ACTIVE, isActive);
        String where = Track.TRACK_ID + "=?";
        String[] args = {trackId+""};
        boolean result = db.update(Track.TABLE_NAME, values, where, args);
        db.close();
        return  result;
    }

    @Override
    public boolean commit(int trackId, boolean isCommit) {
        db.openDataBase();
        ContentValues values  = new ContentValues();
        values.put(Track.COMMIT, isCommit);
        String where = Track.TRACK_ID + "=?";
        String[] args = {trackId+""};
        boolean result = db.update(Track.TABLE_NAME, values, where, args);
        db.close();
        return  result;
    }

    @Override
    public List<Track> tracks(boolean commit, boolean isActive) {
        db.openDataBase();
        List<Track> result = new ArrayList<Track>();
        String whereClause = Track.IS_ACTIVE +"=?";
        String[] whereArgs = {isActive ? "1":"0"};
        Cursor cursor = db.select(true,Track.TABLE_NAME, null, whereClause, whereArgs, null);
        if(cursor != null) {
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        }
        db.close();
        return result;
    }

    @Override
    public List<Track> tracks(boolean commit) {
        List<Track> result = new ArrayList<Track>();
        db.openDataBase();
        String selection = Track.COMMIT +"=?";
        String[] where = {commit ? "1":"0"};
        Cursor cursor = db.select(true,Track.TABLE_NAME,null,selection,where,null);
        if(cursor != null)
            while (cursor.moveToNext())
                result.add(getItem(cursor));
        db.close();
        return  result;
    }

    @Override
    public List<Track> tracks(Date lastUpdate) {
        db.openDataBase();
        List<Track> result = new ArrayList<Track>();
        String sDate = Support.toString(lastUpdate);
        String whereClause = "datetime("+Track.LAST_TIME+") > datetime(?) and "+ Track.IS_ACTIVE +"=?";
        String[] whereArgs = {sDate,"1"};
        String order =Track.LAST_TIME+" desc";
        Cursor cursor = db.select(true,Track.TABLE_NAME, null, whereClause, whereArgs, order);
        if(cursor != null) {
            Track track;
            while (cursor.moveToNext()) {
                track = getItem(cursor);
                result.add(track);
            }
        }
        db.close();
        return result;
    }

    @Override
    public List<Track> tracksSend() {
        db.openDataBase();
        List<Track> result = new ArrayList<Track>();
        String whereClause = "datetime("+Track.LAST_TIME+") = datetime('1111-11-11 11:11:00') and "+Track.IS_ACTIVE+"=1 and "+Track.COMMIT+" =0";
        Cursor cursor = db.select(true,Track.TABLE_NAME, null, whereClause, null, null);
        if(cursor != null) {
            Track track;
            while (cursor.moveToNext()) {
                track = getItem(cursor);
                result.add(track);
            }
        }
        db.close();
        return result;
    }

    private Track getItem(Cursor cursor){
        Track track = new Track();
       track.setTrackId(cursor.getInt(cursor.getColumnIndex(Track.TRACK_ID)));
       track.setActon(cursor.getString(cursor.getColumnIndex(Track.ACTION)));
       track.setMessage(cursor.getString(cursor.getColumnIndex(Track.MESSAGE)));
       track.setActive(Support.toActive(cursor.getString(cursor.getColumnIndex(Track.IS_ACTIVE))));
       track.setCommit(Support.toActive(cursor.getString(cursor.getColumnIndex(Track.COMMIT))));
       track.setLastTime(Support.toLongDateTime(cursor.getString(cursor.getColumnIndex(Track.LAST_TIME))));
       track.setTask(taskService.task(cursor.getInt(cursor.getColumnIndex(Task.TASK_ID))));

        return track;
    }
}
